<template>
  <el-dialog :visible.sync="value" :before-close="() => $emit('input', false)">
    <template slot="title">
      {{isCreated ? '新增' : '编辑'}}三方平台
    </template>
    <el-form :model="form" ref="refForm" :rules="rules" label-width="180px" @keyup.enter.native="saveForm">
      <el-form-item label="三方平台" prop="channelId">
        <el-select filterable v-model="form.channelId">
          <el-option
            v-for="o in channelList"
            :label="o.channelName"
            :value="o.channelId"
            :key="o.channelId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商户号" prop="merchantCode">
        <el-col :span="16">
          <el-input v-model="form.merchantCode"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="公钥" prop="publicKey">
        <el-col :span="16">
          <el-input value="--" v-if="!roleCheck('withdraw-key-show')&&!isCreated" disabled></el-input>
          <el-input v-else v-model="form.publicKey"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="私钥" prop="privateKey">
        <el-col :span="16">
          <el-input value="--" v-if="!roleCheck('withdraw-key-show')&&!isCreated" disabled></el-input>
          <el-input v-else v-model="form.privateKey"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="请求URL" prop="payUrl">
        <el-col :span="16">
          <el-input value="--" v-if="!roleCheck('withdraw-url-show')&&!isCreated" disabled></el-input>
          <el-input v-else v-model="form.payUrl"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="接收URL" prop="notifyUrl">
        <el-col :span="16">
          <el-input value="--" v-if="!roleCheck('withdraw-url-show')&&!isCreated" disabled></el-input>
          <el-input v-else v-model="form.notifyUrl"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-col :span="16">
          <el-input type="textarea" v-model="form.remark"></el-input>
        </el-col>
      </el-form-item>
      <!--<el-form-item label="自动出款金额">
        <el-col :span="11" :offset="1" :pull="1">
          <el-form-item prop="minAmount">
            <el-input placeholder="元" v-model="form.minAmount">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11" :offset="1">
          <el-form-item prop="maxAmount">
            <el-input placeholder="元" v-model="form.maxAmount">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>

        </el-col>
        <span style="color: #cccccc">用户提款额度达到此区间，则触发自动出款程序</span>
      </el-form-item>-->
      <el-form-item label="当日出款上限" prop="dailyLimit">
        <el-col :span="16">
          <el-input-number
            v-model="form.dailyLimit"
            :min="0"
            :controls="false"
            :precision="2"
            :style="{ width: '100%' }"
            class="global-input-number-left-view"
          >
          </el-input-number>
        </el-col>
      </el-form-item>

    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('input', false)" :loading="loading">取 消</el-button>
      <el-button type="primary" @click="saveForm" :loading="loading">确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addPaymentWithdraw } from '@/services/api/payment';
import { getPaymentManageChannelList } from '@/services/api/recharge';

import { form, factory } from '@/build';

export default {
  name: 'pay-edit-dialog',
  props: {
    rowData: {
      type: Object,
      default: () => {},
    },
    value: {
      type: Boolean,
      default: true,
    },
  },
  mixins: [form, factory],
  data() {
    return {
      form: {
        channelId: undefined,
        merchantCode: undefined,
        publicKey: undefined,
        privateKey: undefined,
        payUrl: undefined,
        notifyUrl: undefined,
        remark: undefined,
        ...this.rowData,
      },
      channelList: [],
      applyData: {},
      rules: {
        channelId: [
          { required: true, message: '三方平台为必选项' },
        ],
        merchantCode: [
          { required: true, message: '商户号为必填项' },
        ],
        publicKey: [
          { required: true, message: '公钥为必填项' },
        ],
        privateKey: [
          { required: true, message: '私钥为必填项' },
        ],
        payUrl: [
          { required: true, message: '请求URL为必填项' },
        ],
        notifyUrl: [
          { required: true, message: '接收URL为必填项' },
        ],
        remark: [
          { required: true, message: '备注为必填项' },
        ],
      },
    };
  },
  computed: {
    isCreated() {
      return !(this.rowData && this.rowData.merchantId);
    },
  },
  methods: {
    saveForm() {
      this.doSave({action: 'saveForm', form: 'refForm'});
    },
  },
  mounted() {
    this.bindSave(addPaymentWithdraw, 'saveForm', 'form', {
      afterSuccess: () => {
        this.$emit('input', false);
        this.$parent.gridSearch();
      },
    });

    this.bindGet(getPaymentManageChannelList, 'channelList');
    this.getAll();
  },
};
</script>

<style scoped>
</style>
